package com.csair.seam.infrastructure.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import com.alibaba.fastjson.JSONObject;
import com.csair.seam.infrastructure.utils.JwtUtil;
import com.csair.seam.infrastructure.web.BaseInterceptor;

@Component
public class JWTInterceptor extends BaseInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
    	JSONObject jsonObject = JwtUtil.verifyToken(request);
    	boolean isOk = jsonObject.getInteger("code") == 0;
    	if(!isOk) {
    		returnJson(response,jsonObject.toJSONString());
    	}
        return isOk;
    }
}
